<?php
 /*
 * 1626cms 友情鏈接
 * ============================================================================
 * 版權所有: 1626網絡，并保留所有權利。
 * 網站地址: http://www.1626.com；
 * ----------------------------------------------------------------------------
 * 這不是一個自由軟件！您只能在不用于商業目的的前提下對程序代碼進行修改和
 * 使用；不允許對程序代碼以任何形式任何目的的再發布。
 * ============================================================================
*/
define('IN_QISHI', true);
require_once('../data/config.php');
require_once('./include/admin_common.inc.php');
require_once(ADMIN_ROOT_PATH.'include/admin_link_fun.php');
require_once(ADMIN_ROOT_PATH.'include/upload_img.php');
$act = !empty($_GET['act']) ? trim($_GET['act']) : 'list';
//鏈接列表
if($act == 'list'){
require_once(ADMIN_ROOT_PATH.'include/page.class.php');
$type_id=$_GET['type_id'];
$category=$_GET['category'];
$key=trim($_POST['key']);
!empty($_POST['perpage'])?(setcookie('perpage',intval($_POST['perpage']))).($perpage=intval($_POST['perpage'])):($perpage= !empty($_COOKIE['perpage'])? $_COOKIE['perpage'] :'10');
$get_link_sql=" where link_id is not null ";
($type_id==1) ? ($get_link_sql.=" and  link_logo=''"):'';
($type_id==2 )? ($get_link_sql.=" and  link_logo<>''"):'';
!empty($category)?($get_link_sql.=" and  category_id=".$category):'';
!empty($key)?($get_link_sql.=" and link_name like '%{$key}%'"):'';
$total_sql="SELECT COUNT(*) AS num FROM ".table('link').$get_link_sql;
$page = new page(array('total'=>get_total($total_sql), 'perpage'=>$perpage));
$currenpage=$page->nowindex;
$offset=($currenpage-1)*$perpage;
$links_list = get_links($offset, $perpage,$get_link_sql);
$smarty->assign('act',$act);//標簽ID
$smarty->assign('links_list',$links_list);//鏈接列表
$smarty->assign('page',$page->show(3));//分頁符
$smarty->assign('type_id',$type_id);//鏈接屬性GET
$smarty->assign('category',$category);//鏈接分類GET
$smarty->assign('key',$key);//關鍵字POST
$smarty->assign('perpage',$perpage);//每頁顯示數量POST
$smarty->assign('get_link_category',get_link_category());//獲取鏈接分類
$smarty->assign('pageheader',"管理中心 - 友情鏈接 - 鏈接列表");//提示標題
$smarty->display('admin_link.htm');
}
//添加鏈接
if($act =='add'){
$id = !empty($_GET['id']) ? trim($_GET['id']) : '';
$smarty->assign('act',$act);
$smarty->assign('get_link_category',$_GET['category_id']);//獲取默認分類
$smarty->assign('get_link_category',get_link_category());//獲取鏈接分類
$smarty->assign('pageheader',"管理中心 - 友情鏈接 - 添加鏈接");
$smarty->display('admin_link_add.htm');
}
//修改鏈接
if($act =='edit'){
$id = !empty($_GET['id']) ? trim($_GET['id']) : '';
if ($id<>"")
{
$smarty->assign('act',$act);
$sql = "select * from ".table('link')." where link_id=".$id;
$edit_links=$db->getone($sql);
$edit_links['link_logo']=($edit_links['link_logo'] and strpos($edit_links['link_logo'],"http://")===0)?$edit_links['link_logo']:($edit_links['link_logo']?$upfiles_dir.$edit_links['link_logo']:'');
$smarty->assign('edit_links',$edit_links);//獲取指點ID的記錄
$smarty->assign('get_link_category',get_link_category());//獲取鏈接分類
$smarty->assign('pageheader',"管理中心 - 友情鏈接 - 修改鏈接");
$smarty->display('admin_link_edit.htm');
}
}
//新增鏈接分類頁面
if($act == 'category_add')
{
$smarty->assign('act',"category");
$smarty->assign('pageheader',"管理中心 - 友情鏈接 - 增加分類");
$smarty->display('admin_link_category_add.htm');
}
//刪除LOGO
if($act =='del_logo'){
$id = !empty($_GET['id']) ? trim($_GET['id']) : '';
if ($id<>"")
{
$sql = "select * from ".table('link')." where link_id=".$id;
$logo=$db->getone($sql);
$sql="update ".table('link')." set link_logo='' where link_id=".$id;
$db->query($sql);
($logo['link_logo'] and strpos($logo['link_logo'],"http://")===0)? '':@unlink($upfiles_dir.$logo['link_logo']);
showmsg("刪除LOGO成功！",2);
}
}
//鏈接分類
if($act == 'category')
{
$smarty->assign('act',$act);
$smarty->assign('get_link_category',get_link_category());
$smarty->assign('pageheader',"管理中心 - 友情鏈接 - 鏈接分類");
$smarty->display('admin_link_category.htm');
}
//保存添加鏈接
if($act =='addsave'){
$link_name = !empty($_POST['link_name']) ? trim($_POST['link_name']) : showmsg('鏈接名稱不能為空',1);
$link_url = (!empty($_POST['link_url']) and $_POST['link_url']<>"http://")? trim($_POST['link_url']) : showmsg('鏈接地址填寫不正確',1);
$category_id =trim($_POST['category_id']);
$Notes =trim($_POST['Notes']);
$show_order =trim($_POST['show_order']);
$link_logo =trim($_POST['http_logo'])=="http://" ? '' : trim($_POST['http_logo']);
($link_logo=="" and $_FILES['logo']['name']) ? $link_logo=_asUpFiles($upfiles_dir, "logo", 1024*2, 'jpg/gif/png',true) :'';
$sql = "INSERT INTO ".table('link ')." (link_name,category_id,link_url,show_order,link_logo,Notes) VALUES  ('$link_name','$category_id','$link_url','$show_order','$link_logo','$Notes')";
$db->query($sql);
$link[0]['text'] = "繼續添加友情鏈接";
$link[0]['href'] = 'admin_link.php?act=add&category_id='.$category_id;
$link[1]['text'] = "返回友情鏈接列表";
$link[1]['href'] = 'admin_link.php';
showmsg("添加成功！",2,$link);
}
//保存修改鏈接
if($act =='editsave'){
$link_name = !empty($_POST['link_name']) ? trim($_POST['link_name']) : showmsg('鏈接名稱不能為空',1);
$link_url = (!empty($_POST['link_url']) and $_POST['link_url']<>"http://")? trim($_POST['link_url']) : showmsg('鏈接地址填寫不正確',1);
$category_id =trim($_POST['category_id']);
$link_id =trim($_GET['link_id']);
$Notes =trim($_POST['Notes']);
$show_order =trim($_POST['show_order']);
$link_logo =trim($_POST['http_logo'])=="http://" ? '' : trim($_POST['http_logo']);
($link_logo=="" and $_FILES['logo']['name']) ? $link_logo=_asUpFiles($upfiles_dir, "logo", 1024*2, 'jpg/gif/png',true) :'';
$sql="update ".table('link')." set link_name='".$link_name."',category_id='".$category_id."',link_url='".$link_url."',show_order='".$show_order."',link_logo='".$link_logo."',Notes='".$Notes."' where link_id='$link_id'";
$db->query($sql);
$link[0]['text'] = "返回友情鏈接列表";
$link[0]['href'] = 'admin_link.php';
$link[1]['text'] = "查看修改結果";
$link[1]['href'] = '?act=edit&id='.$link_id;
showmsg("修改成功！",2,$link);
}
//刪除鏈接或移動鏈接（批量/單個）
if($act == 'migrate_del')
{
$y_id=$_REQUEST['y_id'];
$category=$_POST['category'];
$migrate=$_POST['migrate'];
$del_Submit=$_POST['del_Submit'];
if ($y_id=="")
{
showmsg("請選擇項目！",1);
exit;
}
//批量刪除鏈接
if ($del_Submit<>"" and is_array($y_id))
{
$y_num=count($y_id);
for($i=0;$i<$y_num;$i++)
{
$sql_img="select * from ".table('link')." where link_id=".$y_id[$i];
$logo=$db->getone($sql_img);
($logo['link_logo'] and strpos($logo['link_logo'],"http://")===0)? '':@unlink($upfiles_dir."/".$logo['link_logo']);
$db->query("Delete from  ".table('link')." where link_id=".$y_id[$i]);
}
showmsg("刪除成功！",2);
}
//單個刪除鏈接
if (!is_array($y_id))
{
$sql_img="select * from ".table('link')." where link_id=".$y_id;
$logo=$db->getone($sql_img);
($logo['link_logo'] and strpos($logo['link_logo'],"http://")===0)? '':@unlink($upfiles_dir."/".$logo['link_logo']);
$db->query("Delete from  ".table('link')." where link_id=".$y_id);
showmsg("刪除成功！",2);
}
//批量移動鏈接
if ($migrate<>"" && is_array($y_id))
{
empty($category) ? showmsg("請選擇目標分類！",1) : '';
$y_num=count($y_id);
for($i=0;$i<$y_num;$i++)
{
$db->query("update ".table('link')."  set category_id='".$category."' where link_id=".$y_id[$i]);
}
showmsg("轉移成功！",2);
}
}
//保存鏈接分類(所有)
if($act == 'category_all_save')
{
$y_id=$_POST['y_id'];
$categoryname=$_POST['categoryname'];
$category_order=$_POST['category_order'];
$y_num=count($y_id);
for($i=0;$i<$y_num;$i++)
{
$sql="update ".table('link_category')." set categoryname='".$categoryname[$i]."',category_order='".$category_order[$i]."'  where id='$y_id[$i]'";
$db->query($sql);
}
showmsg("成功修改！",2);
}
//保存新增的分類
if($act == 'add_category_save')
{
!empty($_POST['categoryname']) ? ($categoryname=trim($_POST['categoryname'])) : showmsg("請填寫名稱",1);
$category_order=!empty($_POST['category_order']) ? trim($_POST['category_order']):'0';
$sql = "INSERT INTO ".table('link_category')." (categoryname,category_order) VALUES  ('$categoryname','$category_order')";
$db->query($sql);
$link[0]['text'] = "返回分類管理";
$link[0]['href'] = '?act=category';
$link[1]['text'] = "繼續添加分類";
$link[1]['href'] = "?act=category_add";
showmsg("添加成功！",2,$link);
}
//刪除鏈接分類
if($act == 'del_category')
{
$y_id=$_GET['y_id'];
($y_id<=4) ? showmsg("系統默認分類只可修改不可刪除！",1):'';
$db->query("Delete from  ".table('link_category')." where id=".$y_id);
showmsg("刪除成功！",2);
}
?>
